package com.csu.backmanagesys.Controller;

import com.csu.backmanagesys.Domain.Account;
import com.csu.backmanagesys.Service.AccountService;
import com.csu.backmanagesys.Service.IdAndPasswordService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@RequestMapping("/Accounts")
@Controller
public class AccountController {
    @Autowired
    public AccountService AccountService;

    @Autowired
    public IdAndPasswordService idAndPasswordService;

    @GetMapping("")
    public ModelAndView user(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("Account",AccountService.selectAccount());
        modelAndView.setViewName("userInformation1");
        return modelAndView;
    }

    @PutMapping ("/update")
    public ModelAndView reset_user_password(String userid){
        ModelAndView modelAndView = new ModelAndView();
        String md5Password = DigestUtils.md5DigestAsHex("123456".getBytes());
        idAndPasswordService.updateIdAndPassword(userid , md5Password);
        modelAndView.addObject("Account",AccountService.selectAccount());
        modelAndView.setViewName("userInformation1");
        return modelAndView;
    }

    @PostMapping ("/update")
    public ModelAndView update_user_information(HttpServletRequest httpRequest){
        ModelAndView modelAndView = new ModelAndView();
        Account account = new Account();
        account.setUserid(httpRequest.getParameter("userid"));
        account.setEmail(httpRequest.getParameter("email"));
        account.setFirstName(httpRequest.getParameter("firstname"));
        account.setLastName(httpRequest.getParameter("lastname"));
        account.setStatus(httpRequest.getParameter("status"));
        account.setAddress1(httpRequest.getParameter("addr1"));
        account.setAddress2(httpRequest.getParameter("addr2"));
        account.setCity(httpRequest.getParameter("city"));
        account.setState(httpRequest.getParameter("state"));
        account.setZip(httpRequest.getParameter("zip"));
        account.setCountry(httpRequest.getParameter("country"));
        account.setPhone(httpRequest.getParameter("phone"));
        //System.out.println(account.toString());
        AccountService.updateAccount_information(account);
        modelAndView.addObject("Account",AccountService.selectAccount());
        modelAndView.setViewName("userInformation1");
        return modelAndView;
    }

    //搜索
    @GetMapping("/selectUserInf")
    public ModelAndView selectUserInf(String search){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("Account",AccountService.fuzzySelectAccount(search));
        modelAndView.setViewName("userInformation1");
        return modelAndView;
    }
    //寻找
    @GetMapping("/getUserInf")
    @ResponseBody
    public List<Account> getUserInf(HttpServletRequest req){
//        modelAndView.addObject("oneAccount",AccountService.selectAccountById(userId));
//        modelAndView.setViewName("");
//        return modelAndView;
        String userId = req.getParameter("userId");
        return AccountService.selectAccountById(userId);
    }
}
